<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="field_name" lay-verify="required" required>
                            <option value="role_name">角色名</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="field_value" class="layui-input" placeholder="输入搜索内容"/>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="search" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="test" lay-filter="test"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="auth">权限分配</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="statusDemo">
    <input type="checkbox" lay-filter="editStatus" value="{{d.id}}" lay-skin="switch"
           lay-text="可用|禁用" {{d.status==0?'':'checked'}} style="display: none;"/>
    <p style="display: none;">{{d.status==0?'禁用':'可用'}}</p>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="saveDialog">
    <form id="saveForm" lay-filter="saveForm" class="layui-form model-form">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色名</label>
            <div class="layui-input-block">
                <input name="role_name" placeholder="角色名最多30个字符" class="layui-input"
                       lay-verType="tips" lay-verify="required|role_name" autocomplete="off" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="role_desc" placeholder="描述最多100个字符" lay-verType="tips" lay-verify="role_desc" autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/libs/layui/layui.js"></script>
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'zTree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;

        /* 表单验证 */
        form.verify({
            role_name: function(value, item){
                if(value.length > 30){
                    return '角色名最多30个字符';
                }
            },
            role_desc: function(value, item){
                if(value.length > 100){
                    return '描述最多100个字符';
                }
            }
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#test',
            url: "{{:url('Role/index')}}",
            page: {
                limit:20
            },
            method: 'post',
            contentType: 'application/json',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '</p>'].join(''),
            cols: [[
                {field: 'id',title: 'ID'},
                {field: 'role_name', title: '角色名'},
                {field: 'role_desc', title: '描述'},
                {field: 'created_at', title: '创建时间'},
                {field: 'updated_at', title: '更新时间'},
                {field: 'status', title: '状态', templet: '#statusDemo', width: 100},
                {title: '操作', toolbar: '#barDemo', align: 'center', width: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(search)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(test)', function (obj) {
            if (obj.event === 'edit') { //修改
                showSaveModel(obj.data);
            } else if (obj.event === 'auth') {  //权限管理
                showPermModel(obj.data.id);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(test)', function (obj) {
            if (obj.event === 'add') { // 添加
                showSaveModel();
            }
        });

        /* 显示表单弹窗 */
        function showSaveModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '角色',
                area: ['500px', '320px'],
                maxmin: true,
                resize: true,
                content: $('#saveDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('saveForm', mData);
                    var url = mData ? "{{:url('Role/edit')}}" : "{{:url('Role/add')}}";
                    // 表单提交事件
                    form.on('submit(saveSubmit)', function (data) {
                        var loading = layer.load(3);

                        $.ajax({
                            url: url,
                            type: 'post',
                            data: data.field,
                            success: function (res) {
                                if (res.code != 0) {
                                    layer.msg(res.msg, {icon: 5,time:1000});
                                } else {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1,time:1500});
                                    insTb.reload();
                                }
                                layer.close(loading);
                            }, error: function () {
                                layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                                layer.close(loading);
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 权限管理 */
        function showPermModel(roleId) {
            admin.open({
                title: '权限分配',
                btn: ['保存', '取消'],
                area: ['640px', '500px'],
                content: '<ul id="roleAuthTree" class="ztree"></ul>',
                success: function (layero, dIndex) {
                    var loading = layer.load(3);

                    $.ajax({
                        url: "{{:url('Role/auth')}}",
                        type: 'get',
                        data: {
                            role_id: roleId
                        },
                        success: function (res) {
                            if (res.code != 0) {
                                layer.msg(res.msg, {icon: 5,time:1000});
                            } else {
                                $.fn.zTree.init($('#roleAuthTree'), {
                                    check: {enable: true},
                                    data: {simpleData: {enable: true}}
                                }, res.data);
                            }
                            layer.close(loading);
                        }, error: function () {
                            layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                            layer.close(loading);
                        }
                    });
                    //超出一定高度滚动
                    $(layero).children('.layui-layer-content').css({'max-height': '500px', 'overflow': 'auto'});
                },
                yes: function (dIndex) {
                    var insTree = $.fn.zTree.getZTreeObj('roleAuthTree');
                    var checkedRows = insTree.getCheckedNodes(true);
                    var ids = [];
                    for (var i = 0; i < checkedRows.length; i++) {
                        ids.push(checkedRows[i].id);
                    }

                    var loading = layer.load(3);

                    $.ajax({
                        url: "{{:url('Role/auth')}}",
                        type: 'post',
                        data: {
                            id: roleId,
                            menu_ids: ids.join(',')
                        },
                        success: function (res) {
                            if (res.code != 0) {
                                layer.msg(res.msg, {icon: 5,time:1000});
                            } else {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1,time:1500});
                                insTb.reload();
                            }
                            layer.close(loading);
                        }, error: function () {
                            layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                            layer.close(loading);
                        }
                    });
                    return false;
                }
            });
        }

        /* 修改用户状态 */
        form.on('switch(editStatus)', function (obj) {
            var loading = layer.load(3);
            $.ajax({
                url: "{{:url('Role/editStatus')}}",
                type: 'post',
                data: {
                    id: obj.elem.value,
                    status: obj.elem.checked ? 1 : 0
                },
                success: function (res) {
                    if (res.code != 0) {
                        layer.msg(res.msg, {icon: 5,time:1000});
                    }
                    layer.close(loading);
                }, error: function () {
                    layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                    layer.close(loading);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
